/**
 * 测试辅助函数
 *
 * 提供测试用的工具函数和数据生成器
 */

import type { AppointmentManagement } from '@shared/types/'

/**
 * 创建测试用的预约数据
 */
export function createTestAppointment(overrides?: Partial<AppointmentManagement>): AppointmentManagement {
  const now = new Date()
  const randomId = Math.floor(Math.random() * 1000000)

  return {
    id: randomId,
    sequence_number: `TEST${Date.now()}_${randomId}`,
    passport_number: 'E12345678', // ⚠️ 请修改为实际测试用的护照号
    license_number: '440123199001011234', // ⚠️ 请修改为实际测试用的驾照号
    full_name: '测试用户',
    email: 'test@example.com',
    phone_number: '13800138000',
    issuing_country: 'CN',
    issuing_authority: 'GD',
    service_type: 'DI',
    preferred_dates: JSON.stringify(['25-12-2024', '26-12-2024']), // ⚠️ 请修改为实际的可用日期
    preferred_times: JSON.stringify(['09:00-10:00', '14:00-15:00']),
    priority: 2,
    remarks: '测试预约',
    is_placeholder: false,
    is_monitor: false,
    appointment_status: 'pending',
    office_code: 'HKLO',
    booking_mode: 'manual',
    retry_count: 0,
    created_at: now,
    updated_at: now,
    ...overrides,
  }
}

/**
 * 创建多个测试预约
 */
export function createTestAppointments(count: number, baseOverrides?: Partial<AppointmentManagement>): AppointmentManagement[] {
  return Array.from({ length: count }, (_, i) => {
    return createTestAppointment({
      ...baseOverrides,
      id: (baseOverrides?.id || 0) + i,
      sequence_number: `TEST${Date.now()}_${i}`,
      passport_number: `E1234567${i % 10}`, // 循环使用不同的号码
      full_name: `测试用户${i + 1}`,
      email: `test${i + 1}@example.com`,
      priority: (i % 3) + 1, // 不同的优先级 1, 2, 3
    })
  })
}

/**
 * 打印测试结果
 */
export function printTestResult(result: { success: boolean, referenceNumber?: string, error?: string, selectedDate?: string, selectedTimeslot?: string }, index?: number): void {
  const prefix = index !== undefined ? `[预约 ${index + 1}]` : ''

  if (result.success) {
    console.log(`✓ ${prefix} 成功`)
    if (result.referenceNumber)
      console.log(`  参考号: ${result.referenceNumber}`)
    if (result.selectedDate)
      console.log(`  日期: ${result.selectedDate}`)
    if (result.selectedTimeslot)
      console.log(`  时段: ${result.selectedTimeslot}`)
  }
  else {
    console.log(`✗ ${prefix} 失败: ${result.error || '未知错误'}`)
  }
}

/**
 * 统计测试结果
 */
export function statsTestResults(results: Array<{ success: boolean, error?: string }>): {
  total: number
  success: number
  failed: number
  successRate: number
} {
  const total = results.length
  const success = results.filter(r => r.success).length
  const failed = total - success
  const successRate = total > 0 ? Math.round((success / total) * 100) : 0

  return {
    total,
    success,
    failed,
    successRate,
  }
}
